package com.example.t1.activity;

import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;

import com.example.t1.R;
import com.example.t1.util.ImaUtil;

public class Activity2 extends Activity {
    ImageView imageView1, imageView2, imageView3;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	setContentView(R.layout.activity_activity2);

	imageView1 = (ImageView) findViewById(R.id.imageView1);
	Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
		R.drawable.beautiful);

	imageView2 = (ImageView) findViewById(R.id.imageView2);
	BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(),
		zoomBitmap(BitmapFactory.decodeResource(getResources(),
			R.drawable.ic_launcherqq), 1f));

	imageView3 = (ImageView) findViewById(R.id.imageView3);
	try {
	    Bitmap bitmap2 = BitmapFactory.decodeStream(getResources()
		    .getAssets().open("beautiful.jpeg"));
	    Log.d("tag", bitmap2.getWidth() + "assets��");
	    Log.d("tag", bitmap2.getHeight() + "assets��");
	    // imageView2.setBackground(getDrawable(bitmap2));
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	ImaUtil.displayScreen(this);
    }

    /**
     * 
     * ������drawable ת��Ϊbitmap
     * 
     * @param drawable
     * @return
     * @throws
     * @date��2013-8-30 ����4:12:29
     * @author ��Ѹ zhixun123@gmail.com
     * @version v1.0
     */
    public static Bitmap drawableToBitmap(Drawable drawable) {
	// ȡ drawable �ĳ���
	int w = drawable.getIntrinsicWidth();
	int h = drawable.getIntrinsicHeight();

	// ȡ drawable ����ɫ��ʽ
	Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
		: Bitmap.Config.RGB_565;
	// ������Ӧ bitmap
	Bitmap bitmap = Bitmap.createBitmap(w, h, config);
	// ������Ӧ bitmap �Ļ���
	Canvas canvas = new Canvas(bitmap);
	drawable.setBounds(0, 0, w, h);
	// �� drawable ���ݻ���������
	drawable.draw(canvas);
	return bitmap;
    }

    /**
     * 
     * ������bitmap ����
     * 
     * @param bitmap
     * @param width
     * @param height
     * @return
     * @throws
     * @date��2013-8-30 ����4:12:49
     * @author ��Ѹ zhixun123@gmail.com
     * @version v1.0
     */
    public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
	int w = bitmap.getWidth();
	int h = bitmap.getHeight();
	Matrix matrix = new Matrix();
	float scaleWidth = ((float) width / w);
	float scaleHeight = ((float) height / h);
	matrix.postScale(scaleWidth, scaleHeight);
	Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
	return newbmp;
    }

    /**
     * 
     * ������
     * 
     * @param bitmap
     * @return
     * @throws
     * @date��2013-9-2 ����1:55:32
     * @author ��Ѹ zhixun123@gmail.com
     * @version v1.0
     */
    public BitmapDrawable getDrawable(Bitmap bitmap) {
	BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
	return bitmapDrawable;
    }

    /**
     * 
     * ������bitmap ����
     * 
     * @param bitmap
     * @param width
     * @param height
     * @return
     * @throws
     * @date��2013-8-30 ����4:12:49
     * @author ��Ѹ zhixun123@gmail.com
     * @version v1.0
     */
    public static Bitmap zoomBitmap(Bitmap bitmap, float f) {
	int w = bitmap.getWidth();
	int h = bitmap.getHeight();
	Matrix matrix = new Matrix();
	matrix.postScale(f, f);
	Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
	Log.d("tag", "img2��" + newbmp.getWidth());
	Log.d("tag", "img2��" + newbmp.getHeight());
	return newbmp;
    }

    public Bitmap sd() {
	BitmapFactory.Options options = new BitmapFactory.Options();
	options.inJustDecodeBounds = false;
	options.inSampleSize = 1;
	Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
		R.drawable.beautiful, options);
	Log.d("tag", options.outWidth + "img3��");
	Log.d("tag", options.outHeight + "img3��");
	return bitmap;
    }

}
